package org.szgb.console.ht.electricity.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.szgb.console.ht.electricity.entity.HtElectricityAmmeter;
import org.szgb.console.ht.electricity.mapper.HtElectricityAmmeterMapper;
import org.szgb.console.utils.Page;
import org.szgb.console.utils.UUIDUtil;

import java.util.List;

@Slf4j
@Service
public class HtElectricityAmmeterService {
    private final HtElectricityAmmeterMapper htElectricityAmmeterMapper;

    @Autowired
    public HtElectricityAmmeterService(HtElectricityAmmeterMapper htElectricityAmmeterMapper){
        this.htElectricityAmmeterMapper = htElectricityAmmeterMapper;
    }

    @Transactional(readOnly = true)
    public HtElectricityAmmeter get(String id) {
        return htElectricityAmmeterMapper.get(id);
    }

    @Transactional(readOnly = true)
    public List<HtElectricityAmmeter> findList(HtElectricityAmmeter htElectricityAmmeter) {
        if (htElectricityAmmeter != null && htElectricityAmmeter.getPageDto() != null) {
            Page<HtElectricityAmmeter> page = new Page<>(htElectricityAmmeter.getPageDto());
            page.setFirst((page.getPageNo() - 1) * page.getPageSize());
            htElectricityAmmeter.setPage(page);
        }
        return htElectricityAmmeterMapper.findList(htElectricityAmmeter);
    }

    @Transactional(readOnly = true)
    public Integer getCount(HtElectricityAmmeter htElectricityAmmeter) {
        return htElectricityAmmeterMapper.getCount(htElectricityAmmeter);
    }

    @Transactional()
    public void save(HtElectricityAmmeter htElectricityAmmeter) {
        // 检查实体对象的ID是否为null，判断是否为新实体
        if (htElectricityAmmeter.getId() == null || htElectricityAmmeter.getId().isEmpty()) {
            // 对于新实体，设置创建者信息
            htElectricityAmmeter.setCreatedBy(htElectricityAmmeter.getUpdatedBy());
            // 添加UUID
            htElectricityAmmeter.setId(UUIDUtil.getUUID());
            // 执行插入操作
            htElectricityAmmeterMapper.insert(htElectricityAmmeter);
            // 返回成功插入的信息
        } else {
            // 执行更新操作
            htElectricityAmmeterMapper.update(htElectricityAmmeter);
        }
    }

    @Transactional
    public void delete(String ids) {
        htElectricityAmmeterMapper.delete(ids.split(","));
    }
}
